package com.prototype.client.managed.activity;

import com.google.gwt.activity.shared.Activity;
import com.google.gwt.place.shared.PlaceController;
import com.google.gwt.requestfactory.shared.EntityProxyId;
import com.google.gwt.requestfactory.shared.RequestContext;
import com.prototype.client.managed.request.ApplicationRequestFactory;
import com.prototype.client.managed.request.SupplierProxy;
import com.prototype.client.managed.request.SupplierRequest;
import com.prototype.client.managed.ui.SupplierDetailsView;
import com.prototype.client.managed.ui.SupplierEditView;
import com.prototype.client.managed.ui.SupplierListView;
import com.prototype.client.managed.ui.SupplierMobileDetailsView;
import com.prototype.client.managed.ui.SupplierMobileEditView;
import com.prototype.client.scaffold.ScaffoldApp;
import com.prototype.client.scaffold.place.CreateAndEditProxy;
import com.prototype.client.scaffold.place.FindAndEditProxy;
import com.prototype.client.scaffold.place.ProxyPlace;
import com.prototype.shared.domain.SupplierType;

public class SupplierActivitiesMapper {

    private final ApplicationRequestFactory requests;

    private final PlaceController placeController;

    public SupplierActivitiesMapper(ApplicationRequestFactory requests, PlaceController placeController) {
        this.requests = requests;
        this.placeController = placeController;
    }

    public Activity getActivity(ProxyPlace place) {
        switch(place.getOperation()) {
            case DETAILS:
                return new SupplierDetailsActivity((EntityProxyId<SupplierProxy>) place.getProxyId(), requests, placeController, ScaffoldApp.isMobile() ? SupplierMobileDetailsView.instance() : SupplierDetailsView.instance());
            case EDIT:
                return makeEditActivity(place);
            case CREATE:
                return makeCreateActivity();
        }
        throw new IllegalArgumentException("Unknown operation " + place.getOperation());
    }

    @SuppressWarnings("unchecked")
    private EntityProxyId<com.prototype.client.managed.request.SupplierProxy> coerceId(ProxyPlace place) {
        return (EntityProxyId<SupplierProxy>) place.getProxyId();
    }

    private Activity makeCreateActivity() {
        SupplierEditView.instance().setCreating(true);
        final SupplierRequest request = requests.supplierRequest();
        Activity activity = new CreateAndEditProxy<SupplierProxy>(SupplierProxy.class, request, ScaffoldApp.isMobile() ? SupplierMobileEditView.instance() : SupplierEditView.instance(), placeController) {

            @Override
            protected RequestContext createSaveRequest(SupplierProxy proxy) {
                request.persist().using(proxy);
                return request;
            }
        };
        return new SupplierEditActivityWrapper(requests, ScaffoldApp.isMobile() ? SupplierMobileEditView.instance() : SupplierEditView.instance(), activity, null);
    }

    private Activity makeEditActivity(ProxyPlace place) {
        SupplierEditView.instance().setCreating(false);
        EntityProxyId<SupplierProxy> proxyId = coerceId(place);
        Activity activity = new FindAndEditProxy<SupplierProxy>(proxyId, requests, ScaffoldApp.isMobile() ? SupplierMobileEditView.instance() : SupplierEditView.instance(), placeController) {

            @Override
            protected RequestContext createSaveRequest(SupplierProxy proxy) {
                SupplierRequest request = requests.supplierRequest();
                request.persist().using(proxy);
                return request;
            }
        };
        return new SupplierEditActivityWrapper(requests, ScaffoldApp.isMobile() ? SupplierMobileEditView.instance() : SupplierEditView.instance(), activity, proxyId);
    }
}
